package day2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

//测试文件复制（先对文件进行读取，再对文件进行写入）
public class TestCopy {
    public static void main(String[] args) {
        File in = new File("d://io//2025.0623.mp4");
        File out = new File("d://io//movie//new2025.0623.mp4");
        if (in.exists()) {
            File parentOut = out.getParentFile();
            if (!parentOut.exists()) parentOut.mkdirs();

            //统计当前系统时间毫秒
            long start = System.currentTimeMillis();

            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                fis = new FileInputStream(in);
                fos = new FileOutputStream(out);
                byte[] bs = new byte[1024];
                int len = 0;
                while ((len = fis.read(bs)) != -1) {
                    fos.write(bs, 0, len);
                }

                //再统计一次系统时间毫秒数
                long end = System.currentTimeMillis();

                System.out.println("复制成功后：" + (end - start) + "ms");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }finally {
                try {
                    if(fis!=null) fis.close();
                    if(fos!=null) fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            System.out.println("要复制的文件不存在");
        }
    }
}
